#!/bin/bash
date_file=`date +%Y%m%d`
echo 日期:$date_file
#目录变量
#item=/JP_item
item=UFC-JP_item_CBT
home_pash=/data/www
p_dir=$home_pash/$item/game_SVN
g_dir=$home_pash/$item/game_GIT
g_dir_code=$g_dir/gslserver2
game_path=$home_pash/$item/game
gameblank_path=$home_pash/$item/game_line
game_rsync_path=$home_pash/$item/game_rsync
update_code_path=$home_pash/$item/update_code
md5_file_path=$update_code_path/md5_home
release_path=$update_code_path/release
hosts_path=$update_code_path/hosts_ip
log_path=$update_code_path/log
cp_path=$update_code_path/cphome
cp_rsync_path=$cp_path/cprsync
git_url=git@192.168.1.7:capstone/igoal2_server.git

#本地变量
svn_site_path=$update_code_path/svn_site
file_list_path=$release_path/file_list
u_file_list=file_list
svn_site=svn_site
file_site=file_site
ct_1=./tables
ct_2=./json
ct_3=./workerman
release_ip=10.10.20.57
ssh_port=22

#————————————————————————————————————————————————

rsync_path_file=$update_code_path/rsync/rcc.pad
git_path=$gameblank_path/battle/server/app

rsync_md5_file=$md5_file_path/rsync_md5_home
rsync_file=$file_list_path/rsync_file_list
rsync_game_name=$item
rsync_path=$update_code_path/rsync
rsync_ip=$release_ip
rsync_port=10873

_arr_1=($p_dir $g_dir $game_path $gameblank_path $game_rsync_path $update_code_path $file_list_path $rsync_md5_file $rsync_file)
_arr_2=($md5_file_path $release_path $hosts_path $log_path $cp_path $cp_rsync_path $rsync_path $svn_site_path)


function pash_list
{
                for (( i==0;i<${#_arr_1[@]};i++ ))
                do
                        if [ ! -d ${_arr_1[i]} ];then
                        echo ${_arr_1[i]}
                        /bin/mkdir -p ${_arr_1[i]}
                        fi
                done
                for (( j==0;j<${#_arr_2[@]};j++ ))
                do
                        if [ ! -d ${_arr_2[j]} ];then
                        echo ${_arr_2[j]}
                        /bin/mkdir -p ${_arr_2[j]}
                        fi
                done
#if [ ! -f "cp_$item.sh" ];then
#	cp ./cp_united.sh ./cp_$item.sh
#fi
}

#pash_list

#echo 版本：     列表
#ls -l $release_path/$date_file
#exec 1>> 1.log

#更新版本库md5值
function md5_file
{
#cd $game_path
#find ./ -type f -print0 | xargs -0 md5sum > $update_code_path/mygame.md5
echo ""
echo 更新版本库MD5文件:
scp root@$release_ip:$md5_file_path/mygame.md5 $md5_file_path/
cd $gameblank_path
find ./ -type f -print0 | xargs -0 md5sum > $md5_file_path/mygameb.md5
}

#更新游戏目录
function upgame
{

grep -v ".svn" $md5_file_path/mygameb.md5 | grep -v ".git" | sort >$md5_file_path/mygameb_t.md5
grep -v ".svn" $md5_file_path/mygame.md5 | grep -v ".git" | sort >$md5_file_path/mygame_t.md5

diff $md5_file_path/mygame_t.md5 $md5_file_path/mygameb_t.md5 | grep ">" > $cp_path/cp_c
diff $md5_file_path/mygame_t.md5 $md5_file_path/mygameb_t.md5  > $cp_path/cp
cat $cp_path/cp | awk "{print $ 3}" > $cp_path/cpp
cat $cp_path/cp_c | awk "{print $ 3}" > $cp_path/cpp_c
sort $cp_path/cpp |uniq > $cp_path/cppp
sort $cp_path/cpp_c |uniq > $cp_path/cppp_c
grep -v '^$' $cp_path/cppp | grep -v "^$ct_1" | grep -v "^$ct_2" |grep -v "^$ct_3" > $cp_path/cpppp
grep -v '^$' $cp_path/cppp_c | grep -v "^$ct_1" | grep -v "^$ct_2" |grep -v "^$ct_3" > $cp_path/cpppp_c
echo ------ALL DIFF FILE LIST-----
cat $cp_path/cpppp
echo ""
echo ------UPDATE FILE LIST-----
cat $cp_path/cpppp_c
echo ""
h=`cat $cp_path/cpppp | wc -l`
hc=`cat $cp_path/cpppp_c | wc -l`
echo ------UPDATE FILE-----
echo ""
        for ((i=1; i<=$hc; i++))
                do
                path_update=`cat $cp_path/cpppp_c | awk NR==$i |cut -b 2-`
                echo $gameblank_path$path_update $game_path$path_update
        done
echo ""
echo ------CLEAR FILE LIST-----
        for ((i=1; i<=$h; i++))
                do

                        for ((j=1; j<=$hc; j++))
                                do
                                path=`cat $cp_path/cpppp | awk NR==$i |cut -b 2-`
                                path_c=`cat $cp_path/cpppp_c | awk NR==$j |cut -b 2-`

                                if [ $path = $path_c ];then
                                #echo $gameblank_path$path $game_path$path
                                break
                                fi

                        done

                hc_1=$[$hc + 1]
                if [ $j -eq $hc_1 ];then
                echo clear: $game_path$path
                fi

        done

}


#更新本地游戏rsync目录

function upgame_rsync
{
cd $game_rsync_path
find ./ -type f -print0 | xargs -0 md5sum > $rsync_md5_file/rsync_g_md5.md5
cd $gameblank_path
find ./ -type f -print0 | xargs -0 md5sum > $rsync_md5_file/mygameb.md5

grep -v ".svn" $rsync_md5_file/mygameb.md5 | grep -v ".git" | sort >$rsync_md5_file/mygameb_t.md5
grep -v ".svn" $rsync_md5_file/rsync_g_md5.md5 | grep -v ".git" | sort >$rsync_md5_file/rsync_g.md5

md5_rsync_c=`cat $rsync_md5_file/rsync_g.md5 | wc -l`
diff $rsync_md5_file/rsync_g.md5 $rsync_md5_file/mygameb_t.md5 | grep ">" > $cp_rsync_path/cp_c
diff $rsync_md5_file/rsync_g.md5 $rsync_md5_file/mygameb_t.md5 > $cp_rsync_path/cp
cat $cp_rsync_path/cp | awk "{print $ 3}" > $cp_rsync_path/cpp
cat $cp_rsync_path/cp_c | awk "{print $ 3}" > $cp_rsync_path/cpp_c
sort $cp_rsync_path/cpp |uniq > $cp_rsync_path/cppp
sort $cp_rsync_path/cpp_c |uniq > $cp_rsync_path/cppp_c
#grep -v '^$' $cp_rsync_path/cppp > $cp_rsync_path/cpppp

cat $cp_rsync_path/cppp | grep -v '^$' | grep -v '^-' | grep -v "^$ct_1" | grep -v "^$ct_2" |grep -v "^$ct_3" > $cp_rsync_path/cpppp
cat $cp_rsync_path/cppp_c | grep -v '^$' | grep -v '^-' | grep -v "^$ct_1" | grep -v "^$ct_2" |grep -v "^$ct_3" > $cp_rsync_path/cpppp_c

#echo ------ALL DIFF FILE LIST-----
#cat $cp_rsync_path/cpppp
#echo ""
#echo ------UPDATE FILE LIST-----
#cat $cp_rsync_path/cpppp_c
#echo ""
#echo ------CLEAR FILE LIST-----
rm -rf $rsync_file/cl_f
touch $rsync_file/cl_f
h=`cat $cp_rsync_path/cpppp | wc -l`
hc=`cat $cp_rsync_path/cpppp_c | wc -l`
        if [ $md5_rsync_c -gt 100 ];then
                diff $cp_rsync_path/cpppp $cp_rsync_path/cpppp_c | grep '<' >$rsync_file/cl_f_one
                cat $rsync_file/cl_f_one | cut -b 4- >$rsync_file/cl_f
        fi
#cat $rsync_file/cl_f
}


#更新本地游戏目录

function up_rsync_game
{
echo ------UPDATE FILE-----
echo ""
        for ((i=1; i<=$hc; i++))
                do
                path_update=`cat $cp_rsync_path/cpppp_c | awk NR==$i |cut -b 2-`
                dir_path=`dirname $path_update`
                if [ ! -d $game_rsync_path$dir_path ];then
                        mkdir -p $game_rsync_path$dir_path
                fi
                #echo $gameblank_path$path_update "->"  $game_rsync_path$path_update
                cp -r $gameblank_path$path_update $game_rsync_path$path_update
        done
echo ""
echo ------CLEAR FILE-----
        #cat $release_path/$u_file_list/cl_f
        cl=`cat $rsync_file/cl_f | grep -v ^$ | wc -l`
        for ((i=1; i<=$cl; i++))
                do
                path_cl=`cat $rsync_file/cl_f | grep -v ^$ | awk NR==$i |cut -b 2-`
                #echo clear: $game_rsync_path/$path_cl
                rm -rf $game_rsync_path/$path_cl

        done
        echo ""
}


#生成版本差异目录，.tar文件，
function release_list
{
cd $gameblank_path
find ./ -type f -print0 | xargs -0 md5sum > $md5_file_path/mygameb.md5

grep -v ".svn" $md5_file_path/mygameb.md5 | grep -v ".git" | sort >$md5_file_path/mygameb_t.md5
grep -v ".svn" $md5_file_path/mygame.md5 | grep -v ".git" | sort >$md5_file_path/mygame_t.md5

diff $md5_file_path/mygame_t.md5 $md5_file_path/mygameb_t.md5 | grep ">" > $cp_path/cp_c
diff $md5_file_path/mygame_t.md5 $md5_file_path/mygameb_t.md5  > $cp_path/cp
md5_c=`cat $md5_file_path/mygame_t.md5 | wc -l`
cat $cp_path/cp | awk "{print $ 3}" > $cp_path/cpp
cat $cp_path/cp_c | awk "{print $ 3}" > $cp_path/cpp_c
sort $cp_path/cpp |uniq > $cp_path/cppp
sort $cp_path/cpp_c |uniq > $cp_path/cppp_c
#grep -v '^$' $cp_path/cppp > $cp_path/cpppp
#grep -v '^$' $cp_path/cppp_c > $cp_path/cpppp_c
grep -v '^$' $cp_path/cppp | grep -v "^$ct_1" | grep -v "^$ct_2" |grep -v "^$ct_3" > $cp_path/cpppp
grep -v '^$' $cp_path/cppp_c | grep -v "^$ct_1" | grep -v "^$ct_2" |grep -v "^$ct_3" > $cp_path/cpppp_c
echo ------ALL DIFF FILE LIST-----1
cat $cp_path/cpppp
echo ""
echo ------UPDATE FILE LIST-----2
cat $cp_path/cpppp_c
rm -rf $release_path/$u_file_list/up_f
cat $cp_path/cpppp_c | cut -b 2- > $release_path/$u_file_list/up_f
echo ""
h=`cat $cp_path/cpppp | wc -l`
hc=`cat $cp_path/cpppp_c | wc -l`
echo ------生成版本LIST:-----3
        for ((i=1; i<=$hc; i++))
                do
                path_update=`cat $cp_path/cpppp_c | awk NR==$i |cut -b 2-`
                dir_path=`dirname $path_update`
                        #echo $dir_path
                        #echo $release_path/$Nb$dir_path
                #if [ ! -d $release_path/$date_file/$Nb$dir_path ];then
                #       mkdir -p $release_path/$date_file/$Nb$dir_path
                #fi
                        echo  $release_path/$date_file/$Nb$path_update
                        #cp -r $gameblank_path$path_update $release_path/$date_file/$Nb$path_update

        done
echo ""
echo ------CLEAR FILE LIST-----4
rm -rf $release_path/$u_file_list/cl_f
touch $release_path/$u_file_list/cl_f
        if [ $md5_c -gt 100 ];then
                if [ $hc -eq 0 ];then
                        cat $cp_path/cpppp |cut -b 2- > $release_path/$u_file_list/cl_f
                else
                diff $cp_path/cpppp $cp_path/cpppp_c | grep '<' | cut -b 4-  >$release_path/$u_file_list/cl_f
                fi
        fi
        cat $release_path/$u_file_list/cl_f
        echo ""
}


function upgame_y
{
h=`cat $cp_path/cpppp | wc -l`
hc=`cat $cp_path/cpppp_c | wc -l`
#echo ------生成版本LIST:-----3
        for ((i=1; i<=$hc; i++))
                do
                path_update=`cat $cp_path/cpppp_c | awk NR==$i |cut -b 2-`
                dir_path=`dirname $path_update`
                if [ ! -d $release_path/$date_file/$Nb$dir_path ];then
                        mkdir -p $release_path/$date_file/$Nb$dir_path
                fi
                        #echo  $release_path/$date_file/$Nb$path_update
                        cp -r $gameblank_path$path_update $release_path/$date_file/$Nb$path_update

        done
echo ""
}

#本地game_rsync目录MD5值
function game_md5_price
{
echo -n rsync_game目录:$rsync_game_path 的MD值:' '
cd $game_rsync_path
find ./ -type f -print0 | xargs -0 md5sum > $rsync_md5_path/game_md5_price.md5
cat $rsync_md5_path/game_md5_price.md5 | sort > $rsync_md5_path/md5_price.md5
#echo -e "\033[31m `md5sum $rsync_md5_path/md5_price.md5 | awk '{print $1}'` \033[0m"
echo `md5sum $rsync_md5_path/md5_price.md5 | awk '{print $1}'`

}


#本地库game_rsync与版本库game_rsync目录做md5值对比
function game_rsync_md5
{
echo "+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++"
echo 版本库game_rsync
ssh -p $ssh_port $release_ip $update_code_path/cp_ctl.sh -m
echo "---------------------------------------------------------------"
echo 本地库game_rsync
game_md5_price
echo "+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++"
md5_release=`ssh -p $ssh_port $release_ip $update_code_path/cp_ctl.sh -m | awk '{print $3}'`
md5_5=`game_md5_price | awk '{print $3}'`

echo "========================="
if [ $md5_release == $md5_5 ];then
echo -e "\033[31m "MD5对比版本库正常:OK""====" \033[0m"
fi
echo "========================="
}

#本地库game_rsync同步版本game_rsync目录之后做md5对比
function game_rsync_and_md5
{
#upgame_rsync
#up_rsync_game
rsync --port $rsync_port -avzP  --delete --password-file=$rsync_path_file $game_rsync_path/  gameuser@$release_ip::$rsync_game_name | grep -v xfer
#rsync -avzP  --delete --password-file=$rsync_path_file $game_rsync_path/  gameuser@$release_ip::$rsync_game_name
echo ""
echo ""
game_rsync_md5
}



function release
{
release_list
last_continue "检查差异文件列表:"
upgame_y
}

function git_game {
echo game_SVN下版本目录:
cat -n $update_code_path/$svn_site/$file_site
echo -n 请输入顺序号，如果并没有选项，是新的SVN分支请输入数字"0":
read n
svn_url=`cat $update_code_path/$svn_site/$file_site | awk NR==$n`
echo $svn_url
if [ -z "$svn_url" ];then
echo -n 请输入新的svn分支地址:
read svn_url
fi
#echo -n 请输入SVN分支地址:
#read svn_url
grep -q "$svn_url" $update_code_path/$svn_site/$file_site||echo "$svn_url">>$update_code_path/$svn_site/$file_site
game_nm=`basename $svn_url`
echo SVN分支目录: $game_nm
last_continue "Is the SVN right?"
git_rb

}

function git_game_g {

#echo -n 请输入git_版本:::
#read git_version
#last_continue "Is the git-VERSION right?"
git_rb_g

}

function git_rb_branch
{
	echo '分支列表:::'
	git fetch
	git branch -r
        echo  当前分支::
        git branch
        echo -n 请输入git_版本分支名称:::
        read git_branch_name
        last_continue "是否确定:::(y/n)"
	git branch $git_branch_name $item/$git_branch_name
#        git checkout -b $git_branch_name $item/$git_branch_name
	git checkout $git_branch_name
	git pull
        echo "checkout $git_url ::::::::: $git_branch_name "

}


function git_rb_g
{

version=$git_version

if [ ! -d $g_dir_code ]; then
	mkdir $g_dir_code
        cd $g_dir_code
        git init
        git remote add -f $item $git_url
        git config core.sparsecheckout true
        echo '/gameserver/*' >> .git/info/sparse-checkout
        echo '/manage/*' >> .git/info/sparse-checkout
        echo '/goserver/*' >> .git/info/sparse-checkout
        echo '/matchserver/*' >> .git/info/sparse-checkout
	git_rb_branch
else

	cd $g_dir_code
	git_rb_branch
fi

if [ -z $version ]; then
	git pull
else

	git fetch --prune --tags
	git checkout $version
fi

echo git dir: $(pwd)
echo core dir: $core_path
echo -e "\ngit update version[$version] time: $(date)"
git_sh_g
scp -r $g_dir_code/* $gameblank_path/
}

function git_sh_g
{
echo -n 是否更新battle:
read git_yn
if [ $git_yn = "y" ];then
sh $g_dir_code/matchserver/server/app/get_coregame_cn.sh
fi
}




function svn_game {
echo game_SVN下版本目录:
ls -l $p_dir | awk '{print $9}'
echo -n 请输入SVN分支目录:
read svn_game
cd $p_dir/$svn_game
rm -rf $gameblank_path/*
scp -r $p_dir/$svn_game/* $gameblank_path/
}


function git_rb
{
        if [ ! -d $p_dir/$game_nm ];then
                mkdir -p $p_dir/$game_nm
		svn checkout --depth=empty $svn_url $p_dir/$game_nm
                sleep 3
                cd $p_dir/$game_nm
                svn up gameserver  manage  matchserver goserver LogBiServer
                git_sh
        else
                cd $p_dir/$game_nm
                svn up
                git_sh
        fi
rm -rf $gameblank_path/*
scp -r $p_dir/$game_nm/* $gameblank_path/
}


function git_rsync_game
{
git_game_g
game_rsync_and_md5
cgrb_rs
}

function git_sh
{
echo -n 是否更新battle:
read git_yn
if [ $git_yn = "y" ];then
sh $p_dir/$game_nm/matchserver/server/app/get_coregame_cn.sh
fi
}


#传送版本差异.tar包，生成日期目录，差异路径，
function send_Nb
{
echo 当前日期版本号列表:
ls -l $release_path/$date_file/ | awk '{print $9}'
echo 无--
echo -n 输入要更新的版本号:
read Nb
    #echo $Nb >>$LOGFILE
last_continue "Is the revision right?"
cd $release_path/$date_file
tar -cf $Nb.tar $Nb
echo 检查版本库服___务器文件路径
ssh -p $ssh_port  $release_ip /data/www/JP_item/update_code/cp_ctl.sh
echo 上传版本差异包:
scp $release_path/$date_file/$Nb.tar $release_ip:$release_path/$date_file
scp -r $release_path/$u_file_list/ $release_ip:$release_path/
}


#更新差异版本到版本库时时game目录
function update_release
{
echo 更新版本库:
ssh -p $ssh_port  $release_ip $update_code_path/cp_ctl.sh -u

}

#更新T服
function update_t
{
ssh -p $ssh_port $release_ip $update_code_path/cp_ctl.sh -t

}


function update_w
{
ssh -p $ssh_port $release_ip $update_code_path/cp_ctl.sh -w

}

function update_f
{
ssh -p $ssh_port $release_ip $update_code_path/cp_ctl.sh -f

}
function update_g
{
ssh -p $ssh_port $release_ip $update_code_path/cp_ctl.sh -g

}
function update_x
{
ssh -p $ssh_port $release_ip $update_code_path/cp_ctl.sh -x

}

#php_code_run 执行远程PHP脚本 redis缓冲。
#---------------------------------------------------------------
function php_code_run
{
ssh -p $ssh_port $release_ip $update_code_path/cp_ctl.sh -p
}

#____________________________________________________
#版本服备份
function cgrb_s
{
ssh -p $ssh_port  $release_ip $update_code_path/cp_ctl.sh -c

}

function cgrb_rs
{
ssh -p $ssh_port  $release_ip sh $update_code_path/cgrb.sh $game_nm
}


#版本回档
function rd_game
{
ssh -p $ssh_port  $release_ip $update_code_path/cp_ctl.sh -r
}

#向生产服发送命令执行脚本，同步游戏更新备份目录
#向生产服发送命令执行脚本，计算md5值从 "游戏更新备份目录" 更新到 "测试目录"

function ios_y
{
ssh -p $ssh_port  $release_ip $update_code_path/cp_ctl.sh -s

}
function iosjb_y
{
ssh -p $ssh_port  $release_ip $update_code_path/cp_ctl.sh -j

}

function apk_y
{
ssh -p $ssh_port  $release_ip $update_code_path/cp_ctl.sh -k
}



#更新T服
function integrate
{
cgrb_s
git_game
Nb
Nb_admin

}


#选择操作
function operate
{
echo -n "请根据选项:
S):svn一键同步版本|
t):更新测试服|
输入操作::"
read ot
        #echo $ot >>$LOGFILE
        last_continue "That all right?"
        case "$ot" in
#        s) echo svn更新本地，合并数据; git_game ;;
        S) echo svn一键同步版本; git_rsync_game ;;
#        v) echo 切换svn版本目录到库共享目录; svn_game;;
#        c) echo 创建版本函数; Nb ;;
#        a) echo 更新版本库服务器game目录; Nb_admin ;;
        t) echo 更新测试T服; update_t ;;
#        w) echo 更新官网服; update_w ;;
#        f) echo 更新封测服; update_f ;;
#        o) echo 更新IOS服; ios_y ;;
#        j) echo 更新IOSy越狱服; iosjb_y ;;
#        p) echo 更新APK服; apk_y ;;
#        g) echo 更新公测APK服; update_g;;
#        x) echo 更新公测应用宝服; update_x;;
#        b) echo 生成版本目录备份并生成"md5"文件; cgrb_s ;;
#        r) echo 服务器代码回档; rd_game ;;
#        y) echo 同步生产备份更新目录; server_y ;;
#        u) echo 更新生产:; server_update;;
#        i) echo 一键更新版本:; integrate;;
#	p) echo 执行前端服务器redis缓冲同步:; php_code_run;;
        esac
}

#输入版本号
function Nb
{
echo $date_file 当前时间差异包目录：
ls -l $release_path/$date_file/ | awk '{print $9}'
echo "无"
echo -n 输入要生成的差异包号:
read Nb
    echo $Nb >>$LOGFILE
last_continue "Is the revision right?"
rm -rf $release_path/$date_file/$Nb
#echo $release_path/$date_file/$Nb
echo ""
mkdir -p $release_path/$date_file/$Nb
echo 生成差异版本:$Nb
release
last_continue "ALL right?"
}


function Nb_admin
{
send_Nb
last_continue "ALL right?"
echo ""
update_release
}


#等待检察，判断
function last_continue()
{
    echo -n "$@(y/n [n]):"
    read ret
    #echo $ret >>$LOGFILE
    if [ $ret != "y" ]
    then
        killall tail
        echo "stop release"
        exit -1
    fi
}


#echo ""
#echo ""
#echo _____UPDATE GAME_____
#echo GAME目录:$game_path
#echo GAME_BACK目录:$gameblank_path

#create_update_GAME
#create_release
#date_file
#file_list
#svn_site
#log_path
#md5_file_path
#md5_file

#执行主程
RELEASE_IP=server.wisly.org
#game_rsync_and_md5
rsync  -avzP  --delete  $GAME_SROUCE_PATH/  capstone@$RELEASE_IP::$GAME_REMOTE_PATH | grep -v xfer

